export const typeNameMixin = {
    methods: {
        async addTypeNameCnName(/* @type{Array} */list, params){
            let hasBelong = false;
            let hasType = false;
            let hasName = false;
            if(list && list.length > 0){
                const item = list[0];

                for(const label in item){
                    if(label === 'type'){
                        hasBelong = true;
                    }else if(label === 'typeId' || label === 'type_id'){
                        hasType = true;
                    }else if(label === 'nameId' || label === 'name_id'){
                        hasName = true;
                    }
                }

                if(hasBelong){
                    const map = {
                        '1': '造船',
                        '2': '修船',
                        '3': '配套',
                        '4': '海工',
                    };

                    list.forEach(item => {
                        item['typeCnName'] = map[`${item.type}`];
                    });
                }

                return Promise.all([
                    hasType ? this.$axios
                            .get('/admin/boat/data/typeList', {
                                params,
                            }).then((res) => {
                                const {result} = res;
                                const map = {};
                                if(result){
                                    result.forEach(item => {
                                        map[`${item.id}`] = item.name;
                                    });
                                } 
                                return map;
                            }).catch(e => {}) : Promise.resolve(null),

                    hasName ? this.$axios
                            .get('/admin/boat/data/nameList',{
                                params,
                            }).then((res) => {
                                const {result} = res;
                                const map = {};
                                if(result){
                                    result.forEach((item) => {
                                        map[`${item.id}`] = item.name;
                                    });
                                }
                                return map;
                            }): Promise.resolve(null),

                ]).then(([map1, map2]) => {
                    return list.map(item => {
                        if(hasType){
                            item['typeIdCnName'] = map1[`${item.typeId || item.type_id}`] || '';
                        }
                        if(hasName){
                            item['nameIdCnName'] = map2[`${item.nameId || item.name_id}`] || '';
                        }

                        return item;
                    });

                });

            }else{
                return Promise.resolve(list);
            }



        }
    }
};